package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.enums.ResultCodeEnum;
import com.example.common.enums.RoleEnum;
import com.example.entity.Blog;
import com.example.exception.CustomException;
import com.example.mapper.BlogMapper;
import com.example.service.AdminService;
import com.example.service.BlogService;
import com.example.service.CategoryService;
import com.example.service.UserService;
import com.example.utils.TokenUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements BlogService {

    @Resource
    private UserService userService;
    @Resource
    private CategoryService categoryService;
    @Resource
    private AdminService adminService;

    @Override
    public void add(Blog blog) {
        Blog item = getBaseMapper().selectOne(buildWrapper(blog));
        if (!Objects.isNull(item)) {
            throw new CustomException(ResultCodeEnum.NAME_EXIST_ERROR);
        }
        getBaseMapper().insert(blog);
    }

    @Override
    public void deleteById(Integer id) {
        getBaseMapper().deleteById(id);
    }

    @Override
    public void deleteBatch(List<Integer> ids) {
        getBaseMapper().deleteBatchIds(ids);
    }

    @Override
    public Blog selectById(Integer id) {
        return getBaseMapper().selectById(id);
    }

    @Override
    public List<Blog> selectAll(Blog blog) {
        List<Blog> list = getBaseMapper().selectList(buildWrapper(blog));

        list.stream()
                .distinct()
                .forEach(item -> {
                    item.setCategoryName(categoryService.selectById(item.getCategoryId()).getName());
                    item.setUserName(userService.selectById(item.getUserId()).getName());
                });
        return list;
    }

    @Override
    public Page<Blog> selectPage(Blog blog, Integer pageNum, Integer pageSize) {
        Page<Blog> page = new Page<>(pageNum, pageSize);
        Page<Blog> blogPage = getBaseMapper().selectPage(page, buildWrapper(blog));
        blogPage.getRecords().stream()
                .distinct()
                .forEach(item -> {
                    item.setCategoryName(categoryService.selectById(item.getCategoryId()).getName());
                    item.setUserName(userService.selectById(item.getUserId()).getName());
                });

        return blogPage;
    }

    @Override
    public List<Blog> selectTop() {
        List<Blog> blogList = this.selectAll(null);
        blogList = blogList.stream().sorted((b1, b2) -> b2.getReadCount().compareTo(b1.getReadCount()))
                .limit(20)
                .collect(Collectors.toList());
        return blogList;
    }

    public LambdaQueryWrapper<Blog> buildWrapper(Blog blog) {
        if (Objects.isNull(blog)) return null;
        LambdaQueryWrapper<Blog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(blog.getTitle()), Blog::getTitle, blog.getTitle());
        return queryWrapper;
    }
}
